//
//  DataService.m
//  Project
//
//  Created by 冯东辉 on 2018/9/11.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "DataService.h"
#import "HandleNetData.h"

@implementation DataService

+(DataService*)sharedInstance {
    
    static DataService *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[DataService alloc] init];
        
    });
    
    return sharedInstance;
}

#pragma mark 网络请求封装操作

/**
 Post 请求
 
 @param methodDic 当前业务所需的数据
 @param cacheDurationInterval 缓存时间[缓存有效期]
 @param timeout 超时时间
 @param urlMehtod 请求地址
 @param result 结果返回
 */
-(void)postDataByMethods:(id )methodDic
       withCacheDuration:(enum enumCacheDuration)cacheDurationInterval
                 timeout:(NSTimeInterval)timeout
               urlMehtod:(NSString *)urlMehtod
                callBack:(void(^)(NetModel *  object))result {
    
    [[NetWork sharedInstance] asyncPostHttpRequest:methodDic timeout:timeout urlMethod:urlMehtod successed_handler:^(NSDictionary *json) {
        
        result([HandleNetData successData:json]);
        
    } failed_handler:^(NSInteger statusCode, NSData *data) {
        
        result([HandleNetData requestFalse]);
        return;
    }];
    
    return;
}

/**
 Get 请求
 
 @param methodDic 当前业务所需的数据
 @param cacheDurationInterval 缓存时间[缓存有效期]
 @param timeout 超时时间
 @param urlMehtod 请求地址
 @param result 返回结果
 */
-(void)getDataByMethods:(id )methodDic
      withCacheDuration:(enum enumCacheDuration)cacheDurationInterval
                timeout:(NSTimeInterval)timeout
              urlMehtod:(NSString *)urlMehtod
               callBack:(void(^)(NetModel *  object))result {
    
    [[NetWork sharedInstance] asyncGetHttpRequest:methodDic timeout:timeout urlMethod:urlMehtod successed_handler:^(NSDictionary *json) {
        
        result([HandleNetData successData:json]);
        
    } failed_handler:^(NSInteger statusCode, NSData *data) {
        
        result([HandleNetData requestFalse]);
        return;
    }];
    
}

/**
 Post 带文件请求
 
 @param methodDic 当前业务所需的数据
 @param fileData 文件数据
 @param fileName 文件名称
 @param cacheDurationInterval 缓存时间[缓存有效期]
 @param timeout 超时时间
 @param urlMehtod 请求地址
 @param result 返回结果
 */
-(void)postDataByMethods:(id )methodDic
                fileData:(id )fileData
                fileName:(NSString *)fileName
       withCacheDuration:(enum enumCacheDuration)cacheDurationInterval
                 timeout:(NSTimeInterval)timeout
               urlMehtod:(NSString *)urlMehtod
                callBack:(void(^)(NetModel *  object))result {
    
    [[NetWork sharedInstance] asyncPostHttpRequest:methodDic fileData:fileData fileName:fileName timeout:timeout urlMethod:urlMehtod successed_handler:^(NSDictionary *jsonDic) {
        
        result([HandleNetData successData:jsonDic]);
        
    } failed_handler:^(NSInteger statusCode, NSData *data) {
        
        result([HandleNetData requestFalse]);
    }];
}


@end
